<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="600" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="120px" label-position="left">
			<el-form-item label="设置名称" prop="title">
				<el-input v-model="form.title" clearable></el-input>
			</el-form-item>
			<el-form-item label="上传码" prop="code">
				<el-input v-model="form.code" clearable></el-input>
			</el-form-item>
			<el-form-item label="存储方式" prop="location">
				<sc-select v-model="form.location" dic="uploadLocation" clearable filterable></sc-select>
			</el-form-item>
			<el-form-item label="上传大小限制" prop="maxSize">
				<el-input v-model="form.maxSize" clearable>
					<template #append>MB</template>
				</el-input>
			</el-form-item>
			<el-form-item label="上传目录" prop="catalogueId" >
				<catalogue-tree ref="calTree" @selectOne="handleSelectOne" :params="calParam" ></catalogue-tree>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaving" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	import CatalogueTree from "@/components/xunheng/uploadCatalogueTree";
	export default {
		components: {CatalogueTree},
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				calParam:{

				},
				visible: false,
				isSaving: false,
				//表单数据
				form: {
					id:"",
					code:"",
					title: "",
					catalogueId:"",
					location:""
				},
				//验证规则
				rules: {
					code: [
						{required: true, message: '请输入上传码'}
					],
					maxSize: [
						{required: true, message: '请输入上传大小限制'}
					],
					title: [
						{required: true, message: '请输入设置名称'}
					],
					location: [
						{required: true, message: '请选择存储方式',trigger:"change"}
					],
					catalogueId: [
						{required: true, message: '请选择上传目录',trigger:"change"}
					],
				},
			}
		},
		mounted() {
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaving = true;
						var res;
						if(this.mode == 'edit')res = await this.$API.file.uploadSetting.edit.post(this.form)
						else if(this.mode == 'add')res = await this.$API.file.uploadSetting.save.post(this.form)
						this.isSaving = false;
						if(res.success){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}
					}
				})
			},
			handleSelectOne(data){
				this.form.catalogueId = data.id;
			},
			//表单注入数据
			setData(data){
				this.form.id = data.id
				this.form.code = data.code
				this.form.title = data.title
				this.form.location = data.location
				this.form.maxSize = data.maxSize
				this.$nextTick(() => {
					this.$refs.calTree.setData({title:data.catalogueTitle,id:data.catalogueId})
				})
			}
		}
	}
</script>

<style>
</style>
